import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
    state: {
        userName: '',
		TOKEN: '',
		timestampDiff: 0,
		Language: 0,
		Config: {}
    },
    mutations: {
		setDropDownbox(state, payload) {
		  state.DROPDOWNBOX = payload
		},
		setUserName(state, payload) {
		  state.USERNAME = payload
		},
		setName(state, payload) {
		  state.NAME = payload
		},
		setUserType(state, payload) {
		  state.USERTYPE = payload
		},
		setUserId(state, payload) {
		  state.USERID = payload
		},
		setSystemId(state, payload) {
		  state.SYSTEMID = payload
		},
		setMyBrowser(state, payload) {
		  state.MYBROWSER = payload
		},
		setAuthority(state, payload) {
		  state.authority = payload
		},
		setToken(state, payload) {
		  state.TOKEN = payload
		  // dropBox()
		},
		setTimestampDiff(state, payload) {
		  state.timestampDiff = payload
		},
		setLogoSwitch(state, payload) {
		  state.LOGOSWITCH = payload
		},
		setButtonAuth(state, payload) {
		  state.buttonAuth = payload
		},
		setEventId(state, payload) {
		  state.eventId = payload
		},
		setLanguage(state) {
		  state.Language = state.Language === 1 ? 0 : 1
		  window.v.$i18n.locale = state.Language === 1 ? 'zh' : 'en'
		},
		setConfig(state, payload) {
		  state.Config = { ...state.Config, ...payload }
		},
    },
	actions: {
	    /**
	     * @function 登录方法
	     * @param {String} username 用户名
	     * @param {Number} id 用户ID
	     * @param {String} token TOKEN
	     * @param {String} name 用户姓名
	     * @param {Number} systemId 系统ID
	     * @param {Array} authority 用户权限列表
	     * @description 不用分开赋值，只要把后端返回的res.data丢到变量里即可
	     */
	    Login(context, {
	      loginName, id, token, name, systemId, resourceData,
	    }) {
	      context.commit('setUserName', loginName)
	      context.commit('setUserId', id)
	      context.commit('setToken', token)
	      context.commit('setSystemId', systemId)
	      const authBase64 = base64decode(resourceData)
	      const authBase641 = decrypt(authBase64, 'One@-!Sports456G', 'One@-!SportsG654')
	      // 使用Store 方法操作共享状态
	      const authority = JSON.parse(authBase641).sort((a, b) => parseInt(a.sort, 10) - parseInt(b.sort, 10))
	      context.commit('setAuthority', authority)
	      context.commit('setName', name)
	      sessionStorage.setItem('NAME', name) // 用户名
	      sessionStorage.setItem('TOKEN', token) // TOKEN
	      sessionStorage.setItem('USERID', id) // 用户名ID
	      sessionStorage.setItem('SYSTEMID', systemId) // 系统ID
	      sessionStorage.setItem('USERNAME', loginName) // 用户账号
	      sessionStorage.setItem('authority', changeStr(base64encode(JSON.stringify(authority)))) // 权限
	    },
	    /**
	     * @function 退出登录
	     */
	    LoginOut(context) {
	      context.commit('setUserName', '')
	      context.commit('setUserId', '')
	      context.commit('setToken', '')
	      context.commit('setSystemId', '')
	      // 使用Store 方法操作共享状态
	      context.commit('setAuthority', '')
	      context.commit('setName', '')
	      sessionStorage.setItem('NAME', '') // 用户名
	      sessionStorage.setItem('TOKEN', '') // TOKEN
	      sessionStorage.setItem('USERID', '') // 用户名ID
	      sessionStorage.setItem('SYSTEMID', '') // 系统ID
	      sessionStorage.setItem('USERNAME', '') // 用户账号
	      sessionStorage.setItem('authority', changeStr(base64encode(JSON.stringify([])))) // 权限
	    },
	    /**
	     * @function 切换语言
	     * @param {Number} language 语言0为中文，1为英文，默认中文
	     */
	    setLanguage(context) {
	      context.commit('setLanguage')
	    },
	    /**
	     * @function 设置配置
	     * @param {Number} timestampDiff
	     */
	    setConfig(context, {
	      timestamp,
	      verificationSwitch,
	    }) {
	      context.commit('setConfig', {
	        verificationSwitch,
	      })
	      const timestampDiff = new Date().getTime() - timestamp
	      context.commit('setTimestampDiff', timestampDiff)
	    },
	    /**
	     * @function 设置下拉到VUEX
	     * @param {Array} data 获取到的下拉列表
	     */
	    setDict(context, {
	      data,
	    }) {
	      sessionStorage.setItem('DROPDOWNBOX', JSON.stringify(data))
	      context.commit('setDropDownbox', data)
	    },
	    /**
	     * @function 设置按钮权限到VUEX
	     * @param {Array} data 当前页面的按钮权限数组
	     */
	    setButtonAuth(context, data) {
	      context.commit('setButtonAuth', data)
	      sessionStorage.setItem('buttonAuth', JSON.stringify(data))
	    },
	},
})

export default store